package database;

import java.util.LinkedList;

import model.Setting;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBAdapter {
	
	//Variable BD
	private static final String SETTING_TABLE = "setting";
	 
	private static final String DATABASE_NAME = "freeconso_android";
	private static final int DATABASE_VERSION = 8;
	 
	//Creation BD: REQUEST
	private static final String CREATE_SETTING_TABLE ="CREATE TABLE "+SETTING_TABLE+" ("
		+"id INTEGER PRIMARY KEY AUTOINCREMENT,"
		+"name VARCHAR(50) NOT NULL,"
		+"loginfree VARCHAR(30) NOT NULL,"
		+"pwdfree VARCHAR(30) NOT NULL,"
		+"detect BOOLEAN DEFAULT TRUE"
		+")";
	 
	private final Context mCtx;
	
	public DBAdapter(Context ctx) {
		mCtx = ctx;
	}
	
	public void open() throws SQLException {
		mDbHelper = new DatabaseHelper(mCtx);
		mDb = mDbHelper.getWritableDatabase(); 
	}
	
	public void close() {
		mDbHelper.close();
	}
	
	public long insertSetting(Setting setting) {
		ContentValues initialValues = new ContentValues();
		initialValues.put("name", setting.getName());
		initialValues.put("loginfree", setting.getLogin());
		initialValues.put("pwdfree", setting.getPwd());
		initialValues.put("detect", setting.isDetect());
		return mDb.insert(SETTING_TABLE, null, initialValues);
	}
	
	public int updateSetting(Setting ptc) {
		ContentValues values = new ContentValues();
		values.put("id", ptc.getId_setting());
		values.put("name", ptc.getName());
		values.put("loginfree", ptc.getLogin());
		values.put("pwdfree", ptc.getPwd());
		values.put("detect", ptc.isDetect());
		return mDb.update(SETTING_TABLE, values, "name='"+ptc.getName()+"'", null);
	}
	
	public int deleteSetting(Setting setting) {
		return mDb.delete(SETTING_TABLE, "name='"+setting.getName()+"'", null);
	}
	
	public int countSetting() {
		String[] aTableCollonne = new String[] {"id", "name", "loginfree", "pwdfree", "detect"};
		Cursor objCursor = mDb.query(SETTING_TABLE, aTableCollonne, null, null, null, null, null, null);
		int result = objCursor.getCount();
		objCursor.close();	
		return result;
	}
	
	public Setting getSetting(String name) {
		String[] aTableCollonne = new String[] {"id", "name", "loginfree", "pwdfree", "detect"};
		Cursor objCursor = mDb.query(SETTING_TABLE, aTableCollonne, "name='"+name+"'", null, null, null, null, "1");
		int iPostId = objCursor.getColumnIndex("id");
		int iPostName = objCursor.getColumnIndex("name");
		int iPostLoginWeb = objCursor.getColumnIndex("loginfree");
		int iPostPwdWeb = objCursor.getColumnIndex("pwdfree");
		int iPostDetect = objCursor.getColumnIndex("detect");
		Setting retour = null;
		objCursor.moveToFirst();
		
		if (objCursor != null) {	
			retour = new Setting(objCursor.getString(iPostName));
			retour.setId_setting(objCursor.getInt(iPostId));
			retour.setLogin(objCursor.getString(iPostLoginWeb));
			retour.setPwd(objCursor.getString(iPostPwdWeb));
			boolean done = (objCursor.getInt(iPostDetect) != 0);
			retour.setDetect(done);
			objCursor.close();
		}
		
		return retour;
	}
	
	public LinkedList<Setting> getSettings() {
		String[] aTableCollonne = new String[] {"id", "name", "loginfree", "pwdfree", "detect"};
		Cursor objCursor = mDb.query(SETTING_TABLE, aTableCollonne, null, null, null, null, null, null);
		int iPostId = objCursor.getColumnIndex("id");
		int iPostName = objCursor.getColumnIndex("name");
		int iPostLoginWeb = objCursor.getColumnIndex("loginfree");
		int iPostPwdWeb = objCursor.getColumnIndex("pwdfree");
		int iPostDetect = objCursor.getColumnIndex("detect");
		LinkedList<Setting> list = new LinkedList<Setting>();
		objCursor.moveToFirst();
		
		if (objCursor!=null) {	
			for(int i=0; i<objCursor.getCount();i++){
				Setting retour = new Setting(objCursor.getString(iPostName));
				retour.setId_setting(objCursor.getInt(iPostId));
				retour.setLogin(objCursor.getString(iPostLoginWeb));
				retour.setPwd(objCursor.getString(iPostPwdWeb));
				boolean done = (objCursor.getInt(iPostDetect) != 0);
				retour.setDetect(done);
				list.add(retour);
				objCursor.moveToNext();
			}
		}
		
		return list;
	}
	
	private DatabaseHelper mDbHelper;
	
	private SQLiteDatabase mDb;
	private static final String TAG = "BDAdapter";
	
	private static class DatabaseHelper extends SQLiteOpenHelper {
	 
		DatabaseHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}
	 
		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL(CREATE_SETTING_TABLE);
		}
	 
		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
					+ newVersion + ", which will destroy all old data");
			db.execSQL("DROP TABLE IF EXISTS "+ SETTING_TABLE);
			onCreate(db);
		}
	}
}
